import 'package:bike_ride/api/common_repository.dart';
import 'package:bike_ride/api/req.dart';
import 'package:bike_ride/common/mvvm/base_view_model.dart';
import 'package:bike_ride/common/widget/dialog/m_dialog.dart';
import 'package:bike_ride/common/widget/notifier_widget.dart';
import 'package:bike_ride/main/app.dart';
import 'package:bike_ride/model/cyling/model/unpay_info_m.dart';
import 'package:bike_ride/model/mine/model/finance_stat_m.dart';
import 'package:bike_ride/model/pay/model/overseas_pay_m.dart';
import 'package:bike_ride/model/pay/model/payed_order_detail_m.dart';
import 'package:bike_ride/model/pay/view/pay_box_v.dart';
import 'package:bike_ride/router/routers.dart';
import 'package:dio/dio.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class PayBoxViewModel extends PageViewModel<PayBoxViewState> {
  CancelToken? cancelToken;

  late String orderId;
  late int actionType;
  UnPayInfoM? unPayInfoM;
  DiscountStatM? discountStatM;
  //先付后骑需要带着这个参数 去支付
  ReturnBikeReq? returnBikeReq;
  bool paySuccess = false;
  OverseasPayM? overseasPayM;
  PayedOrderDetailM? payedOrderDetailM;

  bool startTimer = false;

  //是否从cycling页面跳转过来
  bool formCyclingPage(){
    return pageRouteObserver?.getRouteStackPath().contains(Routers.cyclingView)??false;
  }

  @override
  onCreate() {
    cancelToken = CancelToken();
    pageDataModel = PageDataModel();
    unPayInfoM = state.widget.arguments['un_pay'];
    discountStatM = state.widget.arguments['finance_info'];
    orderId = state.widget.arguments['order_id'];
    actionType = state.widget.arguments['action_type']??3;
    returnBikeReq = state.widget.arguments['return_bike_req'];

    if(discountStatM == null){
      getFinanceInfo();
    }
  }

  @override
  onDispose() {
    if (!(cancelToken?.isCancelled ?? true)) {
      cancelToken?.cancel();
    }
    super.onDispose();
  }


  Future getFinanceInfo() async {
    discountStatM = await CommonRepository.getFinanceInfoIdl(
        cancelToken: cancelToken);
    state.notifyChange();
  }

  bool isBalancePay = false;
  Future<bool> balancePay(String? orderId,num? totalAmount) async {
    if(isBalancePay)return false;
    isBalancePay = true;
    final result = await CommonRepository.getBalancePayIdl(
        cancelToken: cancelToken, orderId: orderId,totalAmount: totalAmount,actionType: actionType,returnBikeReq: returnBikeReq);
    paySuccess = result;
    if(result){
      getFinishOrderDetail(orderId);
      getFinanceInfo();
    }
    state.notifyChange();
    isBalancePay = false;
    return paySuccess;
  }

  bool isGetOverseasPay = false;
  Future getOverseasOpenPay(String? orderId,num? totalAmount) async {
    if(isGetOverseasPay)return;
    isGetOverseasPay = true;
    final result = await CommonRepository.getOverseasOpenPayIdl(
        cancelToken: cancelToken, orderId: orderId,totalAmount: totalAmount,actionType: actionType,returnBikeReq: returnBikeReq);
    overseasPayM = result;
    // if(result?.qrCode != null && result!.qrCode != ''){
    //   //显示二维码
    //   state.showPayErCodeDialog();
    // }
    if(result?.secureToken != null && result!.secureToken != ''){
      state.navigateToPayView(result.secureToken!);
    }
    isGetOverseasPay = false;
  }

  bool isGetOverseasH5Pay = false;
  FiuuPayM? fiuuPayM;
  Future getOverseasH5Pay(String? orderId,num? totalAmount,{required num payChanel}) async {
    if(isGetOverseasH5Pay)return;
    isGetOverseasH5Pay = true;
    final result = await CommonRepository.getOverseasH5PayIdl(
        cancelToken: cancelToken,
        orderId: orderId,
        totalAmount: totalAmount,
        actionType: actionType,
        payChanel: payChanel,
        returnBikeReq: returnBikeReq);
    fiuuPayM = result;
    if(result?.payApi != null && result!.payApi != ''){
      state.navigateToPayView(result.payApi!);
    }
    isGetOverseasH5Pay = false;
  }


  Future<bool> getOverseasPayStatus() async {
    String? outTradeNo = overseasPayM?.outTradeNumber ?? fiuuPayM?.outTradeNo;

    if(outTradeNo != null && outTradeNo != ''){
      final result = await CommonRepository.getOverseasPayStatusIdl(
          cancelToken: cancelToken, outTradeNo: outTradeNo);
      if(result){
        startTimer = false;
        paySuccess = true;
        state.successCallBack();
        showPaySuccessDialog();
        getFinishOrderDetail(orderId);
        getFinanceInfo();
      }
      return result;
    }
    return false;
  }



  Future getFinishOrderDetail(String? orderId) async {
    EasyLoading.show();
    final result = await CommonRepository.getFinishOrderDetailIdl(
        cancelToken: cancelToken, orderId: orderId);
    EasyLoading.dismiss();
    payedOrderDetailM = result;
    state.notifyChange();
  }

  /// 请求数据
  @override
  Future<PageViewModel?> requestData({Map<String, dynamic>? params}) async {
    return null;

  }
}